package com.ruoyi.other.api;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.HmacSha;
import com.ruoyi.common.utils.ProjectConstant;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.other.pojo.AppVoiceCallDTO;
import com.ruoyi.other.service.AppOtherService;

import io.swagger.annotations.Api;

@RestController
@RequestMapping("/api/v1/rongCloud")
@Api(tags = "【APP】融云controller")
@CrossOrigin
public class AppRongCloudController {

	@Autowired
	private AppOtherService appOtherService;
	
	
	/**
	 * 	获取融云token
	 * @param request
	 * @return
	 */
	@GetMapping(value = "/getUserToken")
	public AjaxResult getUserToken(HttpServletRequest request) {
		return appOtherService.getUserToken(request);
	}
	
	
	/**
	 * 	修改语音通话状态
	 * @param call.toUserId 	对方用户ID
	 * @param call.status		状态 1=通话中 3=已拒绝 4=未接通 5=已取消 6=网络异常
	 * @param request
	 * @return
	 */
	@PostMapping(value = "/updVoiceCallStatus")
	public AjaxResult updVoiceCallStatus(@RequestBody AppVoiceCallDTO call,HttpServletRequest request) {
		System.out.println("参数打印："+JSONObject.toJSONString(call));
		if(null == call) {
			return AjaxResult.error("请输入参数");
		}
		if(null == call.getUserId()) {
			return AjaxResult.error("请输入发起者ID");
		}
		if(null == call.getToUserId()) {
			return AjaxResult.error("请输入接听者ID");
		}
		if(null == call.getStatus()) {
			return AjaxResult.error("请输入状态");
		}
		if(1 == call.getStatus() && StringUtils.isEmpty(call.getRoomId())) {
			return AjaxResult.error("请输入房间ID");
		}
		return appOtherService.updVoiceCallStatus(call, request);
	}
	
	
	/**
	 * 	融云回调
	 * @param payload
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/notifyRongCloud", method = { RequestMethod.POST, RequestMethod.GET })
	public AjaxResult notifyRongCloud(@RequestBody String payload,HttpServletRequest request) throws Exception {
		System.out.println("融云回调头部值：");
		System.out.println("appKey:"+request.getHeader("appKey"));
		System.out.println("nonce:"+request.getHeader("nonce"));
		System.out.println("timestamp:"+request.getHeader("timestamp"));
		System.out.println("signature:"+request.getHeader("signature"));
		String signature = HmacSha.shaEncode(ProjectConstant.RONG_CLOUD_APP_SECRET+request.getHeader("nonce")+request.getHeader("timestamp"));
		System.out.println("验证签名是否正确："+signature);
		
		System.out.println("融云回调值：");
		System.out.println(payload);
		return appOtherService.notifyRongCloud(payload);
	}
}
